<?php

class Admin_TrocaturmaController extends App_Controller_Action
{
    private $_table     = 'CURSO';
    private $_pk        = 'CUR_ID';
    private $_order_col = 'CUR_NOM';
    private $_order     = 'ASC';
    private $_sta_col   = 'CUR_STA';
    private $obj        = '';
    private $form       = '';
    
    public function init()
    {
        
    }

    public function indexAction() {
        $this->_helper->redirector('edit');
    }
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/chosen/chosen.min.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/chosen/chosen.jquery.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_trocaturma.js?1=5');
        
        $query = ' SELECT SQL_CACHE distinct(a.ALU_ID), a.ALU_NOM '
                .' FROM ALUNO a '
                .' INNER JOIN MATRICULA m ON m.ALU_ID = a.ALU_ID AND MAT_STA = 1 '
                .' INNER JOIN CURSO ON CURSO.CUR_ID = m.CUR_ID AND CUR_STA = 1 AND CUR_ANO = "'.date('Y').'"'
                .' ORDER BY a.ALU_NOM';
        $result = $this->db->fetchAll($query);
        $this->view->aluno = $result;
        
        //$this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if(1){//$this->form->isValid($formData)) {
                    $id = (int) $formData[$this->_pk];//$this->form->getValue($this->_pk);
                    
                    $mat = new Admin_Model_DbTable_Matricula();
                    
                    $data = array('CUR_ID'=> $formData['CUR_ID_TRM']);
                    
                    if($formData['CUR_ID_TRM'] > '' && $formData['ALU_ID'] > 0 && $formData['CUR_ID'] > 0){
                        $mat->update($data, 'ALU_ID = '.$formData['ALU_ID'].' AND CUR_ID = '.$formData['CUR_ID'].' AND MAT_STA = 1 ');
                        echo '<script>alert("Alterado com sucesso a turma!");</script>';
                    } else echo '<script>alert("Selecione corretamente as opções!");</script>';
                    
                    //$this->_helper->redirector('index');
                } else {
                    //$this->form->populate($formData);
                    $this->view->data = $formData;
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                // echo $ex->getMessage();
                //$this->form->populate($formData);
                $this->view->data = $formData;
            }
        } else {
            
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {   
            //$data = array($this->_sta_col => 0);
            //$ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id);
        }       
        echo json_encode(array('ok' => $ok));
    }
    public function buscacursoAction() {
        
        Zend_Loader::loadClass('dominio','library');
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $curso_html = '';
        $turma_html = '';
        $id = $this->_getParam('alu_id', 0);
        if ((int)$id > 0){   
            
            $ok = TRUE; 
            try {
                //$mat = new Admin_Model_DbTable_Matricula();
                //$mat = $mat->fetchRow('MAT_STA = 1 and ALU_ID = '.$id);
                $query = 'SELECT SQL_CACHE CUR_ID FROM MATRICULA WHERE MAT_STA = 1 and ALU_ID = '.$id; 
                $mat = $this->db->fetchRow($query);
                
                if((int) $mat['CUR_ID'] > 0) { 
                    
                    $query = 'SELECT SQL_CACHE CUR_ID,CBS_ID,CUR_TRM,CUR_ANO FROM CURSO WHERE CUR_ID = '. (int) $mat['CUR_ID']; 
                    $cur = $this->db->fetchRow($query);

                    $curso_html = '<input type="hidden" id="CUR_ID" name="CUR_ID" value="'.$cur['CUR_ID'].'" /> '. dominio::getTurma((int)$cur['CUR_ID']) ;

                    $query = 'SELECT SQL_CACHE CUR_ID FROM CURSO '
                            .' WHERE CBS_ID = '.$cur['CBS_ID']
                            .' AND CUR_ID <> '.$cur['CUR_ID']
                            //.' AND CUR_TRM <> '.(int)$cur['CUR_TRM']
                            .' AND CUR_ANO = '.$cur['CUR_ANO']
                            .' AND CUR_STA = 1 '
                            .' ORDER BY CUR_TRM';  
                    
                    $troca = $this->db->fetchAll($query);
                    $turma_html .= '<ul class="listop" style="margin-left: 95px;margin-top: -18px;">'; 
                    foreach($troca as $t) {
                        $turma_html .= '<li><label><input type="radio" name="CUR_ID_TRM" value="'.$t['CUR_ID'].'" required="" /> '.dominio::getTurma($t['CUR_ID']).'</label></li>';
                    }
                    $turma_html .= '</ul>'; 
                    
                }else{
                    $curso_html = 'Aluno sem matrícula.';
                } 
           } catch(Exception $ex) {
                $ok = FALSE;
            }
        }    
        
        echo json_encode(array('ok' => $ok,'curso'=>$curso_html,'turma'=>$turma_html));
    }
    
    
    

}
